import cloud from '@lafjs/cloud'
const dbm = cloud.mongo.db
let data

export default async function (ctx: FunctionContext) {
  data = ctx.body.data
  switch (ctx.body.type) {
    case "teaching-news-notes-list"://班级管理-教学日志-教学动态记事-列表
      return await teachingNewsNotesList()
      break;
    case "teaching-news-notes-save"://班级管理-教学日志-教学动态记事-保存
      return await teachingNewsNotesSave()
      break;
    case "archives-curriculum-list"://授课计划
      return await archivesCurriculumList()
      break;
    case "archives-curriculum-journal-list"://教师档案-教学日志查询
      return await archivesCurriculumJournalList()
      break;
    case "archives-curriculum-journal-save"://教师档案-教学日志保存
      return await archivesCurriculumJournalSave()
      break;
    case "archives-curriculum-journal-del"://教师档案-教学日志删除
      return await archivesCurriculumJournalDel()
      break;
    case "archives-curriculum-save":
      return await archivesCurriculumSave()
      break;
    case "archives-curriculum-update"://课程安排编辑
      return await archivesCurriculumUpdate()
      break;
    case "archives-curriculum-import"://课程安排导入-覆盖
      return await archivesCurriculumImport()
      break;
    case "archives-teaching-details":
      return await archivesTeachingDetails()
      break;
    case "archives-teaching-save":
      return await archivesTeachingSave()
      break;
    case "archives-appendix-list"://教案附表-列表
      return await archivesAppendixList()
      break;
    case "archives-appendix-save"://教案附表-保存
      return await archivesAppendixSave()
      break;
    case "archives-tasks-list": //教学任务书-列表
      return await archivesTasksList()
    case "archives-tasks-save": //教学任务书-保存
      return await archivesTasksSave()
    default:
      break;
  }
}

//教学动态记事列表
async function teachingNewsNotesList() {
  let dbName = "teaching-news-notes"
  const ret = await dbm.collection(dbName).find({ classId: data.classId }).toArray()
  return cloud.utils.requestReturn({ data: ret })
}

//教学动态记事保存
async function teachingNewsNotesSave() {
  let dbName = "teaching-news-notes"

  let res = await cloud.nw.db.findByWhereJson({
    dbName,
    whereJson: {
      classId: data.classId,
      archiveNoteId: data.archiveNoteId,
    },
  });
  if (res && res._id) {
    await cloud.nw.db.update({
      dbName,
      whereJson: {
        classId: data.classId,
        archiveNoteId: data.archiveNoteId,
      },
      dataJson: data
    });
  } else {
    await cloud.nw.db.add({
      dbName,
      dataJson: data,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function archivesTasksSave() {
  let dbName = "archives-tasks"
  await cloud.nw.db.del({
    dbName,
    whereJson: { archivesId: data.archivesId }
  });
  for (let index = 0; index < data.tasks.length; index++) {
    const element = data.tasks[index];
    element.archivesId = data.archivesId //设置档案id
    await cloud.nw.db.add({
      dbName,
      dataJson: element,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function archivesTasksList() {
  let dbName = "archives-tasks"
  const ret = await dbm.collection(dbName).find({ archivesId: data.archivesId }).sort({ classIndex: 1 }).toArray()
  return cloud.utils.requestReturn({ data: ret })
}

async function archivesAppendixSave() {
  let dbName = "archives-appendix"
  await cloud.nw.db.del({
    dbName,
    whereJson: { archivesId: data[0].archivesId }
  });
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    await cloud.nw.db.add({
      dbName,
      dataJson: element,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function archivesAppendixList() {
  let dbName = "archives-appendix"
  const ret = await dbm.collection(dbName).find({ archivesId: data.archivesId }).sort({}).toArray()
  return cloud.utils.requestReturn({ data: ret })
}

async function archivesCurriculumList() {
  let dbName = "archives-curriculum"  //教师档案-授课计划
  const ret = await dbm.collection(dbName).find({ archivesId: data.archivesId }).sort({ classIndex: 1 }).toArray()
  return cloud.utils.requestReturn({ data: ret })
}

async function archivesCurriculumJournalList() {
  let dbName = "archives-teaching-journal"  //教师档案-教学日志
  const ret = await dbm.collection(dbName).find({ archivesId: data.archivesId }).sort({ classIndex: 1 }).toArray()
  return cloud.utils.requestReturn({ data: ret })
}

async function archivesCurriculumSave() {
  let dbName = "archives-curriculum"
  let archivesId = data[0].archivesId
  await cloud.nw.db.del({
    dbName,
    whereJson: { archivesId }
  });
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    await cloud.nw.db.add({
      dbName,
      dataJson: element,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function archivesCurriculumJournalDel() {
  let dbName = "archives-teaching-journal"
  await cloud.nw.db.del({
    dbName,
    whereJson: { _id: data.noteId }
  });
  classNoteDel()
  return cloud.utils.requestReturn({ data: "" })

  //班级管理-教学日志
  async function classNoteDel() {
    await cloud.nw.db.del({
      dbName: "teaching-news-notes",
      whereJson: { archiveNoteId: data.noteId }
    });
  }
}

async function archivesCurriculumJournalSave() {
  let dbName = "archives-teaching-journal"
  if (data._id) {
    let query = { ...data }
    delete query._id
    await cloud.nw.db.update({
      dbName,
      whereJson: { _id: data._id },
      dataJson: query
    });
  } else {
    await cloud.nw.db.add({
      dbName,
      dataJson: data,
      cancelAddTime: false
    });
  }

  return cloud.utils.requestReturn({ data: "" })
}

async function archivesCurriculumUpdate() {
  let dbName = "archives-curriculum"
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    await cloud.utils.updateData(dbName, element)
  }
  return cloud.utils.requestReturn({ data: "" })
}

//导入覆盖
async function archivesCurriculumImport() {
  let dbName = "archives-curriculum"
  await cloud.nw.db.del({
    dbName, whereJson: { archivesId: data[0].archivesId }
  });
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    element.classIndex = Number(element.classIndex)
    await cloud.nw.db.add({
      dbName,
      dataJson: element,
      cancelAddTime: false
    });
  }
  return cloud.utils.requestReturn({ data: "" })
}

async function archivesTeachingDetails() {
  let dbName = "archives-teaching"
  let res = await cloud.nw.db.findByWhereJson({
    dbName,
    whereJson: { archivesId: data.archivesId },
  });
  return cloud.utils.requestReturn({ data: res })
}

async function archivesTeachingSave() {
  let dbName = "archives-teaching"
  await cloud.nw.db.del({
    dbName,
    whereJson: { archivesId: data.archivesId }
  });
  await cloud.nw.db.add({
    dbName,
    dataJson: data,
    cancelAddTime: false
  });
  return cloud.utils.requestReturn({ data: "" })
}
